package net;

import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketFileUploadTest {

    @Test
    public void testServerSocketV1() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务器:服务器启动成功,准备接收客户端请求");
        // 等待客户端请求,改方法会阻塞
        Socket socket = serverSocket.accept();
        if(socket!=null){
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:/upload/a.jpeg"));
            InputStream inputStream = socket.getInputStream();
            byte[] buff = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(buff))!=-1){
                bufferedOutputStream.write(buff, 0, length);
            }
            System.out.println("服务端接收文件成功");

            //服务器给客户端相应数据
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("服务端接收文件成功".getBytes());
            socket.shutdownOutput();
        }
    }


}
